﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CRM.Common
{
   public class DirectoryHelper
    {
        /// <summary>
        /// 是否存在目录
        /// </summary>
        /// <param name="sPath"></param>
        /// <returns></returns>
        public static bool Exits(string sPath)
        {
            return Directory.Exists(sPath);
        }
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="sPath"></param>
        /// <returns></returns>
        public static void CreateDirectory(string sPath)
        {
            //FileStream fs = null;
            try
            {
                if (!Directory.Exists(sPath))
                {
                    Directory.CreateDirectory(sPath);
                }
               //return sPath;
            }
            catch (Exception ex)
            {
               throw;
            }
            finally
            {
                //if (fs != null)
                //    fs.Close();
            }
        }
        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="isDeleteRoot">是否删除根目录</param>
        public static void DeleteDirectory(string sPath, bool isDeleteRoot)
        {
            string[] strFiles = Directory.GetFiles(sPath);
            foreach (string strFile in strFiles)
            {
                try
                {
                    File.Delete(strFile);
                }
                catch { }
            }
            string[] strDirs = Directory.GetDirectories(sPath);
            foreach (string strDir in strDirs)
            {
                try
                {
                    DeleteDirectory(strDir, true);
                }
                catch { }
            }
            if (isDeleteRoot)
            {
                FileInfo f = new FileInfo(sPath);
                f.Attributes = FileAttributes.Normal;
                Directory.Delete(sPath);
            }
        }
        /// <summary>
        /// 获取文件所在目录
        /// </summary>
        /// <param name="fullpath"></param>
        /// <returns></returns>
        public static string GetFilePath(string fullpath)
        {
            return Path.GetDirectoryName(fullpath);
        }
        /// <summary>
        /// 获取站点根路径
        /// </summary>
        /// <returns></returns>
        public static string GetWebBasePath()
        {
            return System.Web.HttpContext.Current.Server.MapPath(@"~");
        }
        /// <summary>
        /// 获取Windows Service根路径
        /// </summary>
        /// <returns></returns>
        public static string GetServiceBasePath()
        {
            string myPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            int i = myPath.LastIndexOf('\\');
            return myPath.Remove(i + 1);
        }
        /// <summary>
        /// 获得当前绝对路径含web和winform应用程序方法
        /// </summary>
        /// <returns></returns>
        public static string GetBasePath()
        {
            if (System.Web.HttpContext.Current != null)
            {
                //如果是web
                return GetWebBasePath();
            }
            else //非web程序引用
            {
                return GetServiceBasePath();
            }
        }
        /// <summary>
        /// 获得当前绝对路径含web和winform应用程序方法
        /// </summary>
        /// <param name="strPath">指定的路径</param>
        /// <returns>绝对路径</returns>
        public static string GetMapPath(string strPath)
        {
            if (strPath.StartsWith("~/"))
                strPath = strPath.Substring(2);
            else if (strPath.StartsWith("/"))
                strPath = strPath.Substring(1);
            if (System.Web.HttpContext.Current != null)
            {
                //如果是web 
                return System.IO.Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~"), strPath);
            }
            else //非web程序引用
            {
                //if (strPath.StartsWith("~/"))
                //    strPath = strPath.Substring(2);
                //else if (strPath.StartsWith("/"))
                //    strPath = strPath.Substring(1);
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }
    }
}
